package com.shanzmoo.stm.admin.web;

import com.shanzmoo.base.bean.SResponse;
import com.shanzmoo.core.cache.CurrentUserCache;
import com.shanzmoo.db.stm.service.IStmSysMenuService;
import com.shanzmoo.db.stm.entity.StmSysMenuEntity;
import com.shanzmoo.base.bean.SHttpResponse;
import com.shanzmoo.db.sys.entity.vo.MenuTreeVo;
import com.shanzmoo.db.sys.entity.vo.MenuVo;
import com.shanzmoo.stm.admin.domain.dto.StmSysMenuSaveDTO;
import com.shanzmoo.stm.admin.domain.dto.StmSysMenuUpdateDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;

import javax.validation.Valid;
import java.util.List;

/**
 * 仓储用户系统菜单 控制层
 *
 * @author Gzhao 2021-03-10
 */
@Api(tags = "系统菜单")
@RestController
@RequestMapping("/sys/menu")
public class StmSysMenuController {

    @Autowired
    private IStmSysMenuService stmSysMenuService;


    @ApiOperation(value = "添加")
    @PostMapping("/add")
    public SHttpResponse<Boolean> add(@Valid @RequestBody StmSysMenuSaveDTO saveDto) {
        StmSysMenuEntity entity = saveDto.convertEntity();
        entity.setCreateUser(CurrentUserCache.getUserId());

        stmSysMenuService.save(saveDto.convertEntity());
        return SHttpResponse.ok(Boolean.TRUE);
    }

    @ApiOperation(value = "删除")
    @GetMapping("/delete")
    public SHttpResponse<Boolean> deleteById(@RequestParam(value = "id") Integer id) {
        SResponse<Boolean> re = stmSysMenuService.deleteById(id, CurrentUserCache.getUserId());

        return SHttpResponse.transform(re);
    }

    @ApiOperation(value = "修改")
    @PostMapping("/update")
    public SHttpResponse<Boolean> update(@Valid @RequestBody StmSysMenuUpdateDTO updateDto) {
        StmSysMenuEntity entity = updateDto.convertEntity();
        entity.setUpdateUser(CurrentUserCache.getUserId());

        stmSysMenuService.updateById(updateDto.convertEntity());
        return SHttpResponse.ok(Boolean.TRUE);
    }

    @ApiOperation(value = "菜单列表树")
    @GetMapping("/allTree")
    public SHttpResponse<List<MenuVo>> getAllTree() {
        SResponse<List<MenuVo>> re = stmSysMenuService.getAllTree();
        return SHttpResponse.transform(re);
    }

    @ApiOperation(value = "简要菜单树（用于菜单选择）")
    @GetMapping("/briefTree")
    public SHttpResponse<List<MenuTreeVo>> getBriefTree() {
        SResponse<List<MenuTreeVo>> re = stmSysMenuService.getBriefTree();
        return SHttpResponse.transform(re);
    }
 }
